package cn.fl.riskctrl.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.riskctrl.service.IRskCtrlCpService;
import cn.fl.system.constant.SysLogConstant;
import fl.constant.facade.framework.constant.DictionaryConstant;
import fl.constant.facade.framework.constant.UploadConstant;
import fl.customer.facade.customer.api.ICustomerQueryApiService;
import fl.customer.facade.customer.dto.CstBscInfoDTO;
import fl.riskctrl.facade.riskctrl.dto.DataResultDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description: 风险控制-控制层
 */
@Controller
@RequestMapping("/riskctrl/rskctrlcp")
public class RskCtrlCpController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(RskCtrlCpController.class);
	
	/**
	 * 风控服务
	 */
	@Autowired
	private IRskCtrlCpService rskCtrlCpService;

	/**客户服务*/
	@Autowired
	public ICustomerQueryApiService customerQueryApiService;
	
	/**
	 * 到征信主页面
	 */
	@RequestMapping(value = "/toMainPage.do", method = { RequestMethod.GET })
	public ModelAndView toMainPage(HttpServletRequest request, HttpServletResponse response) {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "toMainPage", " 到征信主页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		String flag = request.getParameter("flag");//节点标识
		String prjId = request.getParameter("id");//项目ID
		ModelAndView view = new ModelAndView("riskctrl/riskctrlcp/riskCtrlCpMain");
		String path = PropertiesCacheUtils.getProperty(UploadConstant.UPLOAD_SERVER_PATH);
		view.addObject("zx_path", path);
		view.addObject("zx_prjid", prjId);
		view.addObject("zx_flag", flag);
		this.rskCtrlCpService.setCstIdForView(Long.valueOf(prjId), view);//这里要根据项目ID获取客户ID和所有担保人信息
        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "toMainPage", " 到征信主页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
	/**
	 * 获取风控数据
	 */
	@RequestMapping(value = "/getRiskCtrlData.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object getRiskCtrlData(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "getRiskCtrlData", " 获取风控数据.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		String prjId = request.getParameter("prjId");//项目ID
		String cstId = request.getParameter("cstId");//客户ID
		
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if (StringUtils.isEmpty(prjId) || StringUtils.isEmpty(cstId)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("获取征信数据项目ID和客户ID不能为空!");
			BaseLogUtils.info(logger, "getRiskCtrlData", this.getRequestParameter(request));
			return result;
		}
		String cstTypCd = PropertiesCacheUtils.getProperty(DictionaryConstant.CODE_GRP_CST_TYP_CD_CORP);
		CstBscInfoDTO cstBscInfoDTO = this.customerQueryApiService.getCstBscInfoById(Long.valueOf(cstId));
		if (cstTypCd.equals(cstBscInfoDTO.getCstTypCd())) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("被查询人为法人!");
			BaseLogUtils.info(logger, "getRiskCtrlData", this.getRequestParameter(request));
			return result;
		}
		try {
			DataResultDTO data = this.rskCtrlCpService.getRiskCtrlIsCorrect(Long.valueOf(prjId), Long.valueOf(cstId));
			result.setData(data.getData());
		} catch (BaseException ex) {
			BaseLogUtils.error(logger, "-", "异常", ex);
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getRiskCtrlData", "获取数据失败!" + this.getRequestParameter(request) + ", ERROR : " + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			BaseLogUtils.error(logger, "-", "异常", ex);
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getRiskCtrlData", "获取数据失败!" + this.getRequestParameter(request) + ", ERROR : " + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "getRiskCtrlData", " 获取风控数据.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return result;
	}
	
	/**
	 * 获取中智诚数据
	 */
	@RequestMapping(value = "/getZZCIsCorrectData.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object getZZCIsCorrectData(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "getZZCIsCorrectData", " 获取中智诚数据.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		String prjId = request.getParameter("prjId");//项目ID
		String cstId = request.getParameter("cstId");//客户ID
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if (StringUtils.isEmpty(prjId) || StringUtils.isEmpty(cstId)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("获取征信数据项目ID和客户ID不能为空!");
			BaseLogUtils.info(logger, "getZZCIsCorrectData", this.getRequestParameter(request));
			return result;
		}
		String cstTypCd = PropertiesCacheUtils.getProperty(DictionaryConstant.CODE_GRP_CST_TYP_CD_CORP);
		CstBscInfoDTO cstBscInfoDTO = this.customerQueryApiService.getCstBscInfoById(Long.valueOf(cstId));
		if (cstTypCd.equals(cstBscInfoDTO.getCstTypCd())) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("被查询人为法人!");
			BaseLogUtils.info(logger, "getZZCIsCorrectData", this.getRequestParameter(request));
			return result;
		}
		try {
			DataResultDTO data = this.rskCtrlCpService.checkZZCIsCorrect(Long.valueOf(prjId), Long.valueOf(cstId));
			result.setData(data.getData());
		} catch (BaseException ex) {
			ex.getStackTrace();
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getZZCIsCorrectData", "获取数据失败!" + this.getRequestParameter(request) + ", ERROR : " + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			ex.getStackTrace();
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getZZCIsCorrectData", "获取数据失败!" + this.getRequestParameter(request) + ", ERROR : " + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "getZZCIsCorrectData", " 获取中智诚数据.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return result;
	}
	
	/**
	 * 获取信加数据
	 */
	@RequestMapping(value = "/getXjIsCorrectData.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object getXjIsCorrectData(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "getXjIsCorrectData", " 获取信加数据.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		String prjId = request.getParameter("prjId");//项目ID
		String cstId = request.getParameter("cstId");//客户ID
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if (StringUtils.isEmpty(prjId) || StringUtils.isEmpty(cstId)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("获取征信数据项目ID和客户ID不能为空!");
			BaseLogUtils.info(logger, "getXjIsCorrectData", this.getRequestParameter(request));
			return result;
		}
		String cstTypCd = PropertiesCacheUtils.getProperty(DictionaryConstant.CODE_GRP_CST_TYP_CD_CORP);
		CstBscInfoDTO cstBscInfoDTO = this.customerQueryApiService.getCstBscInfoById(Long.valueOf(cstId));
		if (cstTypCd.equals(cstBscInfoDTO.getCstTypCd())) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("被查询人为法人!");
			BaseLogUtils.info(logger, "getXjIsCorrectData", this.getRequestParameter(request));
			return result;
		}
		try {
			DataResultDTO data = this.rskCtrlCpService.checkXjIsCorrect(Long.valueOf(prjId), Long.valueOf(cstId));
			result.setData(data.getData());
		} catch (BaseException ex) {
			ex.getStackTrace();
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getXjIsCorrectData", "获取数据失败!" + this.getRequestParameter(request) + ", ERROR : " + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			ex.getStackTrace();
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getXjIsCorrectData", "获取数据失败!" + this.getRequestParameter(request) + ", ERROR : " + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "getXjIsCorrectData", " 获取信加数据.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return result;
	}

	/**
	 * 获取三方手机认证数据
	 */
	@RequestMapping(value = "/getSynthMobileIsCorrectData.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object getSynthMobileIsCorrectData(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "getSynthMobileIsCorrectData", " 获取三方手机认证数据.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		String prjId = request.getParameter("prjId");//项目ID
		String cstId = request.getParameter("cstId");//客户ID
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if (StringUtils.isEmpty(prjId) || StringUtils.isEmpty(cstId)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("获取征信数据项目ID和客户ID不能为空!");
			BaseLogUtils.info(logger, "getMobileIsCorrectData", this.getRequestParameter(request));
			return result;
		}
		String cstTypCd = PropertiesCacheUtils.getProperty(DictionaryConstant.CODE_GRP_CST_TYP_CD_CORP);
		CstBscInfoDTO cstBscInfoDTO = this.customerQueryApiService.getCstBscInfoById(Long.valueOf(cstId));
		if (cstTypCd.equals(cstBscInfoDTO.getCstTypCd())) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("被查询人为法人!");
			BaseLogUtils.info(logger, "getMobileIsCorrectData", this.getRequestParameter(request));
			return result;
		}
		try {
			DataResultDTO data = this.rskCtrlCpService.checkSynthMobileIsCorrect(Long.valueOf(prjId), Long.valueOf(cstId));
			result.setData(data.getData());
		} catch (BaseException ex) {
			ex.getStackTrace();
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getMobileIsCorrectData", "获取数据失败!" + this.getRequestParameter(request) + ", ERROR : " + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			ex.getStackTrace();
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getMobileIsCorrectData", "获取数据失败!" + this.getRequestParameter(request) + ", ERROR : " + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "getMobileIsCorrectData", " 获取三方手机认证数据.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return result;
	}

	/**
	 * 获取三方综合评分
	 */
	@RequestMapping(value = "/getSynthScoreIsCorrectData.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object getSynthScoreIsCorrectData(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "getSynthScoreIsCorrectData", " 获取三方综合评分.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		String prjId = request.getParameter("prjId");//项目ID
		String cstId = request.getParameter("cstId");//客户ID
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if (StringUtils.isEmpty(prjId) || StringUtils.isEmpty(cstId)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("获取征信数据项目ID和客户ID不能为空!");
			BaseLogUtils.info(logger, "getSynthIsCorrectData", this.getRequestParameter(request));
			return result;
		}
		String cstTypCd = PropertiesCacheUtils.getProperty(DictionaryConstant.CODE_GRP_CST_TYP_CD_CORP);
		CstBscInfoDTO cstBscInfoDTO = this.customerQueryApiService.getCstBscInfoById(Long.valueOf(cstId));
		if (cstTypCd.equals(cstBscInfoDTO.getCstTypCd())) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("被查询人为法人!");
			BaseLogUtils.info(logger, "getSynthIsCorrectData", this.getRequestParameter(request));
			return result;
		}
		try {
			DataResultDTO data = this.rskCtrlCpService.checkSynthScoreIsCorrect(Long.valueOf(prjId), Long.valueOf(cstId));
			result.setData(data.getData());
		} catch (BaseException ex) {
			ex.getStackTrace();
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getSynthIsCorrectData", "获取数据失败!" + this.getRequestParameter(request) + ", ERROR : " + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			ex.getStackTrace();
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getSynthIsCorrectData", "获取数据失败!" + this.getRequestParameter(request) + ", ERROR : " + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "getSynthIsCorrectData", " 获取三方综合评分.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return result;
	}
	
}
